package com.example.designPattern.absfactory.order;

import com.example.designPattern.factory.pizza.Pizza;
import com.example.designPattern.absfactory.factory.AbsFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 订单类
 * 用户从该类选择购买pizza
 */
public class OrderPizza {
    //工厂类
    AbsFactory absFactory;
    //pizza
    Pizza pizza = null;

    public OrderPizza(AbsFactory absFactory){
        this.absFactory = absFactory;
        setFactory();
    }

    public void setFactory(){
        System.out.println("~~ 创建订单~~");

        do{
            String pizzaType = getType();
            Pizza pizza = absFactory.createPizza(pizzaType);
            if (pizza != null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("~~~ 工厂不生产该pizza ~~~ ");
                break;
            }
        }
        while(true);
    }

    //读取pizza输入的类型
    private String getType()  {
        try {
            System.out.print("input need  pizza name ： ");
            String type ;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String s = bufferedReader.readLine();
            return s;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
